package com.zhitong.money.mapper;

import com.zhitong.money.entity.Permission;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface PermissionMapper {

    @Results(id = "permissionMap" ,value={
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "name",property = "name"),
            @Result(column = "type",property = "type"),
            @Result(column = "description",property = "description"),
            @Result(column = "status",property = "status")
    })
    @Select("select * from permission where id = #{id}")
    public Permission findPermissionById(String id);

    @ResultMap("permissionMap")
    @Select("select * from permission")
    public List<Permission> findAllPermission();

    @ResultMap("permissionMap")
    @Select({
            "<script>",
            "select * from permission",
            "<where>",
            "<if test='name!=null and name != \"\"'>and name like concat(concat('%',#{name},'%'))</if>",
            "<if test='type!=null and type != \"\"'>and type=#{type}</if>",
            "<if test='status!=null and status !=\"\"'>and status=#{status}</if>",
            "</where>",
            "</script>"
    })
    public List<Permission> findPermissionByMap(Map<String,Object> map);

    @ResultMap("permissionMap")
    @Select("Select * from permission where id in (select pid from role_permission where rid = #{rid})")
    public List<Permission> findPermissionByRid(String rid);

    @Select("select * from permission where name=#{name}")
    public Permission findPermissionByName(String name);

    @Insert("insert into permission values(#{id},#{name},#{type},#{description},#{status})")
    public void addPermission(Permission permission);

    @Update({
            "<script>",
            "update permission",
            "<trim prefix='set' suffixOverrides=','>",
            "<if test='name!=null and name != \"\"'> name=#{name},</if>",
            "<if test='type!=null and type != \"\"'> type=#{type},</if>",
            "<if test='description!=null and description!=\"\"'> description=#{description},</if>",
            "<if test='status!=null and status!=\"\"'> status=#{status},</if>",
            "</trim>",
            "where id = #{id}",
            "</script>"
    })
    public void updatePermission(Permission permission);

    @Delete("delete from permission where id=#{id}")
    public int delPermission(String id);

    @Delete({
            "<script>",
            "delete from permission",
            "where id in",
            "<foreach item='item' collection='ids' open='(' close=')' separator=','>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    public int delPermissionByIds(List<String> ids);
}
